package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ShopActive;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ShopActivityView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/7/4 0004.
 */

public class ShopActivityPersenter extends BasePresent<ShopActivityView> {

    RetrofitSerives retrofitSerives;

    public void getBrandActivity(int page, int pageSize, Integer shopId) {

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddressTest);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), getParams(page, pageSize, shopId));
        Call<ShopActive> call = retrofitSerives.getShopActive(body);
        call.enqueue(new Callback<ShopActive>() {
            @Override
            public void onResponse(Call<ShopActive> call, Response<ShopActive> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setBrandActivity(response.body());
                        view.hideProgress();
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                        view.showErrorLayout(null);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                    view.showErrorLayout(null);
                }
            }

            @Override
            public void onFailure(Call<ShopActive> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
                view.showErrorLayout(null);
            }
        });

    }

    private String getParams(int page, int pageSize, Integer shopId) {

        JSONObject paramsJson = new JSONObject();
        try {
            paramsJson.put("page", page);
            paramsJson.put("pageSize", pageSize);

            JSONObject param = new JSONObject();
            param.put("shop_id", shopId);

            paramsJson.put("param", param);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return paramsJson.toString();
    }

    @Override
    public void stopRequest() {

    }
}
